package com.zemb.flxwear.UI.Fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;
import com.clj.fastble.scan.BleScanRuleConfig;
import com.zemb.flxwear.Adapter.DeviceAdapter;
import com.zemb.flxwear.BLE.BLEUtils;
import com.zemb.flxwear.R;
import com.zemb.flxwear.UI.Activity.MeasureActivity;
import com.zemb.flxwear.Utils.DeviceUtils;


import java.util.ArrayList;
import java.util.List;

public class ConnectFragment extends BaseFragment {
    RecyclerView rcDevice;
    Button disconnectButton;
    TextView tvDeviceName;
    DeviceAdapter deviceAdapter;


    @Override
    int getLoadingPageId() {
        return R.id.loading_page;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_connect;
    }

    @Override
    void onNotifyCallBack(byte[] data) {

    }


    @Override
    protected void initData() {
        deviceAdapter = new DeviceAdapter(getActivity());
        deviceAdapter.setOnItemClickListener(new DeviceAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, BleDevice device, int position) {
                //设置按钮不能点击
                showLoading(getResourceString(R.string.connecting));
                if ((device.getName() + "").contains(DeviceUtils.BLOOD_PRESSURE)) {
                    //血压设备
                    //跳转到血压测量界面
                    DeviceUtils.putUUId(DeviceUtils.Category.BLOOD_PRESSURE, device.getMac());
                    activity.switchFragment(DeviceUtils.UIType.BLOOD_PRESSURE);
                } else if ((device.getName() + "").contains(DeviceUtils.ECG)) {
                    //心电设备

                    //跳转到心电测量界面
                    DeviceUtils.putUUId(DeviceUtils.Category.ECG, device.getMac());
                    activity.switchFragment(DeviceUtils.UIType.ECG);
                } else if ((device.getName() + "").contains(DeviceUtils.BLOOD_OXYGEN)) {
                    //血氧设备
                    //跳转到血氧测量界面
                    DeviceUtils.putUUId(DeviceUtils.Category.BLOOD_OXYGEN, device.getMac());
                    activity.switchFragment(DeviceUtils.UIType.BLOOD_OXYGEN);
                } else if ((device.getName() + "").contains(DeviceUtils.BLOOD_FOUR_CATEGORY)) {
                    //四合一设备
                    //跳转到血糖测量界面
                    DeviceUtils.putUUId(DeviceUtils.Category.BLOOD_SUGAR, device.getMac());
                    activity.switchFragment(DeviceUtils.UIType.BLOOD_FOUR_CATEGORY);
                }


            }
        });

        ((MeasureActivity) getActivity()).ScanDevice(new BleScanRuleConfig(), new BleScanCallback() {
            @Override
            public void onScanFinished(List<BleDevice> scanResultList) {

            }

            @Override
            public void onScanStarted(boolean success) {

            }

            @Override
            public void onScanning(BleDevice bleDevice) {

                deviceAdapter.addDevice(bleDevice);
            }
        });
    }

    @Override
    public void onStartConnectDevice() {

    }

    @Override
    protected void initView(View root) {
        rcDevice = root.findViewById(R.id.rv_device_list);
        disconnectButton = root.findViewById(R.id.btn_disconnect);
        tvDeviceName = root.findViewById(R.id.tv_device_name);
        tvDeviceName.setText(getResourceString(R.string.disconnect));
        disconnectButton.setVisibility(View.GONE);
        if (BLEUtils.getInstance().IsConnect()) {
            disconnectButton.setVisibility(View.VISIBLE);
        }
        disconnectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((MeasureActivity) getActivity()).disconnectDevice(((MeasureActivity) getActivity()).getCurrentDevice());
                disconnectButton.setVisibility(View.GONE);
            }
        });
        rcDevice.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
        rcDevice.setAdapter(deviceAdapter);


    }

    public void onDeviceDisConnected() {
        tvDeviceName.setText(getResourceString(R.string.disconnect));
        disconnectButton.setVisibility(View.GONE);
    }

    @Override
    public void onMeasureComplete() {

    }

    @Override
    public void onMeasureStart() {

    }

    @Override
    public String getCharacteristicHead() {
        return null;
    }

    @Override
    public void onDeviceConnect(BleDevice bleDevice) {




    }

    @Override
    public void onDeviceConnectFail(BleDevice bleDevice, BleException exception) {

    }

    @Override
    public void onDeviceDisConnected(BleDevice bleDevice) {

    }

    @Override
    public void onResume() {
        super.onResume();
        deviceAdapter.setList(new ArrayList<>());

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }



    @Override
    public void onStop() {
        super.onStop();

    }


    @Override
    void closeNotify() {
       // BleManager.getInstance().stopNotify(activity.getCurrentDevice(), service_uuid, characteristic_uuid);
    }
}
